﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using LingDong.PictureKeywords;
using NLog;
using LingDong.CommonUtility;
using System;

namespace LingDong.PictureKeywordsExample
{
    class PictureKeywordsTest
    {
        #region Test Entry

        internal static void RunDOMTree()
        {
            RunTest(Baseline.GetPictureKeywords, DOMTreeOutputFile);
        }

        internal static void RunVIPS()
        {
            RunTest(Wrapper.GetPictureKeywords, VIPSOutputFile);
        }
        
        internal static void RunVisibility()
        {
            Visibility.Init(visibilityCountOutputFile);
            RunTest(Visibility.GetPictureKeywords, visibilityOutputFile);
        }

        #endregion


        #region Visibility Download

        internal static void RunVisibilityDownload()
        {
            List<string> content = new List<string>();
            using (StreamReader sr = new StreamReader(DOMTreeOutputFile))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    if (String.IsNullOrEmpty(line) ||
                        line.StartsWith(inputDir))
                    {
                        continue;
                    }

                    content.Add(line);
                }
            }

            Visibility.DownloadC1C2FromBaidu(content, visibilityCountOutputFile);
        }

        internal static void RunVisibilityDownloadAgain()
        {
            Dictionary<string, KeyValuePair<int, int>> terms = new Dictionary<string, KeyValuePair<int, int>>();
            using (StreamReader sr = new StreamReader(visibilityCountOutputFile))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] lineSplit = line.Split();
                    int c1 = Convert.ToInt32(lineSplit[1]);
                    int c2 = Convert.ToInt32(lineSplit[2]);
                    terms[lineSplit[0]] = new KeyValuePair<int, int>(c1, c2);
                }
            }

            File.Move(visibilityCountOutputFile, visibilityCountOutputFile + ".bak");
            Visibility.DownloadC1C2FromBaidu(terms, visibilityCountOutputFile);
        }

        #endregion


        #region Private Methods

        private static void RunTest(GetPictureKeywordsMethod method, string outputFile)
        {
            DownloadedPictures.Load(pictureIndexFile);

            string[] files = Directory.GetFiles(inputDir);
            int counter = 0;
            using (StreamWriter sw = new StreamWriter(outputFile, false))
            {
                foreach (string file in files)
                {
                    //if (!file.Contains("qq") && !file.Contains("sina"))
                        //continue;

                    string content = GetNewsContent(file);
                    List<KeyValuePair<string, string>> keywordList = method(file, content);
                    SaveKeywordListToFile(file, keywordList, sw);

                    //if (counter++ == 1000)
                        //return;
                    if (counter % 100 == 0)
                        logger.Debug(counter);
                    counter++;
                }
            }
        }

        private static void SaveKeywordListToFile(string file, List<KeyValuePair<string, string>> keywordList, StreamWriter sw)
        {
            foreach (KeyValuePair<string, string> pair in keywordList)
            {
                sw.WriteLine("{0}\t{1}", file, pair.Key);
                sw.WriteLine(pair.Value);
                sw.WriteLine();
            }
        }
        
        internal static string GetNewsContent(string file)
        {
            string realFile = Path.GetFileName(file);
            if (realFile.StartsWith("ifeng"))
            {
                return Utility.GetFileContent(file, Encoding.UTF8);
            }
            else
            {
                return Utility.GetFileContent(file, Encoding.GetEncoding("gb2312"));
            }
        }

        #endregion


        #region Private Members

        private delegate List<KeyValuePair<string, string>> GetPictureKeywordsMethod(string url, string html);

        private static string inputDir = @"F:\LingDongData\WebPage\TestExple\";
        private static string indexFile = @"F:\LingDongData\WebPage\index.txt";

        private static string DOMTreeOutputFile = @"F:\LingDongData\WebPage\DOMTree_result.txt";
        private static string VIPSOutputFile = @"F:\LingDongData\WebPage\VIPS_result.txt";
        private static string visibilityOutputFile = @"F:\LingDongData\WebPage\visibility_keywords.txt";

        private static string visibilityCountOutputFile = @"F:\LingDongData\WebPage\visibility_count.txt";
        private static string pictureIndexFile = @"F:\LingDongData\WebPage\pic_index.txt";

        private static Logger logger = LogManager.GetCurrentClassLogger();

        #endregion
    }
}
